package com.haiyou.data.common.accessor;

import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;

import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe;
import com.haiyou.common.entity.Prefix;
import com.haiyou.common.event.asyc.AbstractAsyncEventObserver;
import com.haiyou.common.spring.event.SpringApplicationContextReadyEvent;
import com.haiyou.common.spring.util.SpringUtil;
import com.haiyou.common.util.reflect.AnnotationFinder;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AccessorObserver extends AbstractAsyncEventObserver {

	@Subscribe
	public void recv(SpringApplicationContextReadyEvent event) {
		// TODO 检查mysql表命名重复
		java.util.Set<String> set = Sets.newHashSet();
		SpringUtil.getBeansWithAnnotation(Prefix.class).values().forEach(c -> {
			Prefix prefix = AnnotationFinder.findAnnotation(c.getClass(), Prefix.class);
			if (!set.add(prefix.value())) {
				log.error("@前缀重复：" + c, new Throwable());
				System.exit(-1);
			}
		});
	}

}
